﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessMenuMaintain
// 文件功能描述：菜单维护数据访问层
//
// 
// 创建标识：2012-05-11 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessMenuMaintain
    {
        /// <summary>
        /// 获取菜单数量
        /// </summary>
        /// <returns></returns>
        public int GetMenucount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Menus";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取菜单数量
        /// </summary>
        /// <returns></returns>
        public int GetMenucount(string where)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Menus " + where;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelMenuInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY MenuId ASC) rownumber,MenuId,MenuName,case MenuType when 1 then '大荤'" +
                               "when 2 then '小荤' when 3 then '素菜' when 4 then '汤饭点心类' end MenuType,MenuPrice,MenuRemarks " +
                               "from Con_Menus " + strquery + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 保存菜单(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveMenuAdd(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "insert into Con_Menus(MenuName,MenuType,MenuPrice,MenuRemarks) Values('" +
                supp[0] + "','" + supp[1] + "','" + supp[2] + "','" + supp[3] + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteMenu(string ID)
        {
            int rowcount = 0;
            sqldb db = new sqldb();
            string query = "select count(1) from Con_MenuConfiguration where MenuId='" + ID + "'";
            object obj = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            if (Convert.ToInt32(obj) == 0)
            {
                query = "Delete from Con_Menus where MenuId = '" + ID + "'";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            else
            {
                rowcount = 0;
            }
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataSet GetMenuInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from Con_Menus where MenuId = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 保存菜单(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveMenuEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update Con_Menus set MenuName='" + supp[1] + "',MenuType='" + supp[2] + "',MenuPrice=" + supp[3] + ",MenuRemarks='" + supp[4] + "' where MenuId=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }
    }
}
